﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessComponents;


namespace PresentationLayerWebForms
{
    // The reservation base controller acts as the function provider for all pages that participate
    //  in the reservation flow. 
    // This implements the principles of Page Controller pattern. 
    // The all pages in this flow would derive from the class and the derived pages would only implement
    // the strictly presentation level codes.  
    // For any other business function the pages would invoke the methods in this class. 
    // Thus the individual page classes are thin and are not aware of the calls to application layer or 
    // navigational flow.

    public partial class ReservationBaseController : System.Web.UI.Page
    {
        // Navigator keeps the flow map for the reservation 
        // The key is the "currentpage.aspx" string the value is the "nextpage.aspx"
        private Dictionary<string, string> nextPageNavigator;
        private Dictionary<string, string> previousPageNavigator;
        private Dictionary<string, string> nextPageNavigatorRoomTypeSelected;
        private Dictionary<string, string> previousPageNavigatorRoomTypeSelected;
      
        // The current page 'id' would be set in this variable by the deriving page
        protected string currentPage;
        protected bool skipChooseRoomType = false;
        
        // The reservation facade is a Business Interface providing services for accessing BLL functionality 
        protected BusinessComponentsFacade businessComponentsFacade;
       
        // The reservation view object is a strongly typed session state object structure 
        // for storing reservation information that is shared by all pages participating in this flow
        //protected ReservationViewObjects reservationDetails;

        // To be called for intialisation by the deriving class Page_Load
        protected virtual void Page_Load(object sender, EventArgs e)
        {
            // This could be improved by keeping navigator as a singleton
            // This could be improved by using enumerations instead of string
            // Shown here for clarity of purpose for page controller pattern
            nextPageNavigator = new Dictionary<string, string>();
            nextPageNavigator.Add("SelectDate.aspx", "ChooseRoomType.aspx");
            nextPageNavigator.Add("ChooseRoomType.aspx", "GuestsInformation.aspx");
            nextPageNavigator.Add("GuestsInformation.aspx", "Default.aspx");

            previousPageNavigator = new Dictionary<string, string>();
            previousPageNavigator.Add("SelectDate.aspx", "Default.aspx");
            previousPageNavigator.Add("ChooseRoomType.aspx", "SelectDate.aspx");
            previousPageNavigator.Add("GuestsInformation.aspx", "ChooseRoomType.aspx");

            nextPageNavigatorRoomTypeSelected = new Dictionary<string, string>();
            nextPageNavigatorRoomTypeSelected.Add("SelectDate.aspx", "GuestsInformation.aspx");
            nextPageNavigatorRoomTypeSelected.Add("GuestsInformation.aspx", "Default.aspx");

            previousPageNavigatorRoomTypeSelected = new Dictionary<string, string>();
            previousPageNavigatorRoomTypeSelected.Add("SelectDate.aspx", "Default.aspx");
            previousPageNavigatorRoomTypeSelected.Add("GuestsInformation.aspx", "SelectDate.aspx");

            businessComponentsFacade = new BusinessComponentsFacade();

            //if (Session["ReservationDetails"] == null)
            //{
            //    reservationDetails = new ReservationViewObjects();
            //    Session["ReservationDetails"] = reservationDetails;
            //}
            //else
            //    reservationDetails = (ReservationViewObjects)Session["ReservationDetails"];
        }

        // Navigator to be used to move to next step (view) in the process
        protected void NextPage()
        {
            if (skipChooseRoomType)
            {
                Response.Redirect(nextPageNavigatorRoomTypeSelected[currentPage]);
            }
            else
            {
                Response.Redirect(nextPageNavigator[currentPage]);
            }
        }

        protected void LastPage()
        {
            if (skipChooseRoomType)
            {
                Response.Redirect(previousPageNavigatorRoomTypeSelected[currentPage]);
            }
            else
            {
                Response.Redirect(previousPageNavigator[currentPage]);
            }
        }

        protected void FirstPage()
        {
            Response.Redirect("SelectDate.aspx");
        }
    }
}